package cn.faifai.desktop.api;

import cn.faifai.common.component.R;
import cn.faifai.common.page.TableDataInfo;
import cn.faifai.tagapi.dto.PageQuery;
import cn.faifai.tagapi.dto.TagDTO;
import cn.faifai.tagapi.dto.TagSerialize;
import cn.faifai.tagapi.entity.Tag;
import cn.faifai.tagapi.repository.TagRepository;
import cn.faifai.tagapi.service.TagService;
import cn.faifai.tagapi.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Component
public class TagController {
    @Autowired
    private TagRepository tagRepository;
    @Autowired
    private TagService tagService;

    public Tag save(TagDTO tag){
        return tagService.save(tag);
    }

    public R<Tag> update(TagDTO tagDTO) throws IllegalAccessException {
        Tag entity = tagRepository.findById(tagDTO.getId()).orElseThrow(() -> new RuntimeException("Entity not found"));
        BeanUtil.copyPropertiesNonNull(tagDTO,entity);
        return R.ok(tagRepository.save(entity));
    }

    public List<Tag> getOneLayerListByParentId(Long parentId){
        List<Tag> byParentId = tagRepository.findByParentId(parentId);
        return byParentId;
    }

    public R delete(List<Long> ids){
        tagService.delete(ids);
        return R.ok();
    }

    public TableDataInfo<Tag> getTagPageList(TagDTO tagDTO, PageQuery pageQuery){
        return tagService.getTagPageList(tagDTO,pageQuery);
    }

    public TableDataInfo<TagDTO> getTagDTOPageList(TagDTO tagDTO, PageQuery pageQuery){
        return tagService.getTagDTOPageList(tagDTO,pageQuery);
    }

    /**
     * 通过id查询tag
     */
    public TagSerialize getTagById(Long id){
        return tagService.getTagById(id);
    }


    public boolean hasChild(Long tagId){
        return tagService.hasChild(tagId);
    }
}
